//
//  YCPowerViewController.m
//  LOLBox
//
//  Created by mac on 15-4-1.
//  Copyright (c) 2015年 Honey. All rights reserved.
//

#import "YCPowerViewController.h"
#import "HttpRequestManager.h"
#import "YCPowerModel.h"
#import "YCPowerTableViewCell.h"
#import "YCAppDelegate.h"
#import "YCPowerSearchViewController.h"
#import "DataBaseSImple.h"
#import "PublicUINav.h"

@interface YCPowerViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation YCPowerViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self createBackBarBtnWithTarget:self WithAction:@selector(popTheView)];
        [self createNavTitleView:@"战斗力"];
                
        self.navigationItem.rightBarButtonItems = @[[[UIBarButtons alloc] createSpaceButtonWithWidth:-10],[[UIBarButtons alloc] createAddBarButtonWithTarget:self WithAction:@selector(addView)]];
    }
    return self;
}
- (void)popTheView
{
    if (self.dataSource.count > 0)
    {
        NSLog(@"清数据");
        [self.dataSource removeAllObjects];
    }
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewWillAppear:(BOOL)animated
{
    YCAppDelegate *delegate = [UIApplication sharedApplication].delegate;
    [delegate hideTabBar];
    animated = YES;
    
    if (self.dataSource.count > 0)
    {
        NSLog(@"有数据了");
        [self.dataSource removeAllObjects];
    }
    self.dataSource = [[DataBaseSimple shareInstance] selectFromDB];
    
    [self.tableView reloadData];
}
- (void)addView
{
    YCPowerSearchViewController *add = [[YCPowerSearchViewController alloc] initWithNibName:@"YCPowerSearchViewController" bundle:nil];
    PublicUINav *nav = [[PublicUINav alloc] initWithRootViewController:add];
    
    [self presentViewController:nav animated:YES completion:nil];
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initTableView];
//    [[HttpRequestManager shareInstance] downloadZHLWithName:@"中国神油" withServerName:@"电信一"];
    
}

- (void)initTableView
{
    //CGRect rect = [UIScreen mainScreen].bounds;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    
    //self.tableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 90;
    //self.tableView.sectionHeaderHeight = 300;
    //self.tableView.backgroundColor = [UIColor redColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    /**
     *  去掉多余的线
     */
    UIView *v = [[UIView alloc] init];
    v.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = v;
    
    // 注册Cell
    [self.tableView registerNib:[UINib nibWithNibName:@"YCPowerTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:self.tableView];
    
}
#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCPowerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    YCPowerModel *model = self.dataSource[indexPath.row];
    [cell fillCellWithModel:model];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}

//定制删除标题
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCPowerModel *m = self.dataSource[indexPath.row];
    
    [[DataBaseSimple shareInstance] deleFromDBWithPower:[m.zdlSorce integerValue]];
    
    //顺序必须先删数据后处理UI
    //1.先删除数据
    [self.dataSource removeObjectAtIndex:indexPath.row];
    
    //2.在删除cell
    NSArray *paths = @[indexPath]; //NSIndexPath
    [tableView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationAutomatic];
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
